package com.bd.learning.excel;

import cn.hutool.core.io.FileUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ExcelSum {

    public static void main(String[] args) {
        ExcelSum excelSum = new ExcelSum();
        List<BeanObj> totalList = excelSum.getBeanObj("D:\\doc\\测试覆盖率\\测试代码覆盖率提升\\1-全分类接口.xlsx", 0);
        totalList.remove(0);
        excelSum.groupSum(totalList);
    }


    private void groupSum(List<BeanObj> totalList){
        Map<String,Integer> map = new HashMap<>();
        for (BeanObj obj : totalList){
            if("结构体".equals(obj.getType2())){
                continue;
            }
            Integer value = map.get(obj.getServerName());
            Integer orgValue = Integer.parseInt(obj.getMethodNum()) == 0?1:Integer.parseInt(obj.getMethodNum());

            if(value == null){
                map.put(obj.getServerName(),orgValue);
            }else {
                map.put(obj.getServerName(),(value+orgValue));
            }
        }
        System.out.println("key.size="+map.keySet().size());
        int sum = 0;
        for (String key : map.keySet()){
            System.out.println(key+" "+map.get(key));
            sum += map.get(key);
        }
        System.out.println("total="+sum);
    }

    public List<BeanObj> getBeanObj(String file, int index) {
        ExcelReader reader = ExcelUtil.getReader(FileUtil.file(file), index);
        List<List<Object>> readAll = reader.read();
        List<BeanObj> totalList = new ArrayList<>();
        for (List<Object> list : readAll) {
            BeanObj bo = new BeanObj();
            bo.setType1((String) list.get(0));
            bo.setType2((String) list.get(1));
            bo.setPackageName(String.valueOf(list.get(2)));
            bo.setFileName(String.valueOf(list.get(3)));
            bo.setMethodName(String.valueOf(list.get(4)));
            bo.setMethodNum(String.valueOf(list.get(5)));
            bo.setServerName(String.valueOf(list.get(6)));
            totalList.add(bo);
        }
        reader.close();
        return totalList;
    }

}
